// pages/deposit/deposit.js
const app = getApp()
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
          coins:'',//当前金币
          tixian:'',//提现金额
          status:'',//账号状态
          unfinish:'',//订单状态
          sqshow:false,//是否授权
          tixianstatus:false,//提现成功
          todayPayCount:'',//是否提现
          title: "我要提现",//当前页面的标题
          wxcode:'',//微信code  
          mobile:'',//手机号码        
          list:[1,5,10,30,50],
          toastshow:false,//第一次提现金币提现
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
          this.setData({
                  headerheight: app.globalData.navHeight,
          })
  },
  onShow:function(){
          this.request()
  },
        request: function () {
                wx.showNavigationBarLoading()
                wx.showLoading({
                        title: '玩命加载中...',
                })
                var data = {
                        user_id: wx.getStorageSync('userInfo').id
                }
                var header = {
                        'authorization': wx.getStorageSync('tekon'),
                        'content-type': "application/x-www-form-urlencoded"
                }
                util.formhttp(app.globalData.appurl + "/goldconins/mycount", 'get', data, header, this.stat);
        },
        stat: function (res) {
                console.log(res)
                if (res.status == 401) {
                        this.login()
                        return
                }
                wx.hideNavigationBarLoading()
                wx.hideLoading()
                this.setData({
                        rmb: res.data.rmbRat,
                        coins: res.data.coins,
                        status: res.data.status,
                        mobile: res.data.mobile,
                        unfinish: res.data.unfinish,
                        todayPayCount: res.data.todayPayCount
                })
         },
        login: function () {
                var that = this
                wx.login({
                        success: function (res) {
                                var code = res.code
                                var data = {
                                        userCode: code,
                                        userImageUrl: "",
                                        userName: ""
                                }
                                util.promotion(app.globalData.appurl + "/qinqiang/user", "POST", data, that.succeed)
                        }
                })
        },
        succeed: function (res) {
                wx.setStorageSync('tekon', res.data.token)
                wx.setStorageSync('userInfo', res.data.user)
                this.request()

        },
  
        clink:function(e){
                if (this.data.coins >= e.currentTarget.dataset.coins){
                        this.setData({
                                eq: e.currentTarget.dataset.eq,
                                tixian: e.currentTarget.dataset.coins
                        }) 
                }else{
                      wx.showToast({
                              title: '金币不足',
                              image: 'images/nas.png',
                              duration: 2000,
                              mask: true,
                      })
                }
               
        },
        // 提现记录
        tixianjilu:function(){
                wx.navigateTo({
                        url: '/pages/withdraw/withdraw',
                })
        },
        // 获取电话号码
        getPhoneNumber(e) {
                this.setData({
                        sqshow: false
                })
                var data = {
                        code: this.data.wxcode,
                        encryptedData:e.detail.encryptedData,
                        iv: e.detail.iv
                }
                var header = {
                        'authorization': wx.getStorageSync('tekon'),
                        'content-type': "application/x-www-form-urlencoded"
                }
                util.formhttp(app.globalData.appurl + '/goldconins/updatemobile', 'get', data, header, (res)=>{
                        var that=this
                        if (res.message == "success"){
                                this.setData({
                                        mobile:123
                                })
                                wx.showModal({
                                        title: '提示',
                                        content: '确认提现？',
                                        showCancel: true,
                                        success: function (res) {
                                                if (res.confirm) {
                                                        that.forbut()
                                                }
                                        }
                                })  
                        }
                });
               
        } ,
        //提现
        forbut:function(){
                var data = {
                        user_id: wx.getStorageSync('userInfo').id,
                        coins: this.data.tixian
                }
                var header = {
                        'authorization': wx.getStorageSync('tekon'),
                        'content-type': "application/x-www-form-urlencoded"
                }
                util.formhttp(app.globalData.appurl +'/goldconins/pay', 'get', data, header, this.suuc);
        },
        tijiao:function(){
                var that = this
                if (this.data.tixian==""){
                        wx.showToast({
                                title: '请选择提现金额',
                               icon:"none",
                                duration: 2000,
                                mask: true,
                        })
                        return
                }
                if (this.data.unfinish == 1) {
                        wx.showModal({
                                content: "目前还有等待处理的兑换申请",
                                showCancel: false,
                                confirmText: "我知道了"
                        })
                        return
                }
                if (this.data.todayPayCount == 1) {
                        wx.showModal({
                                content: "系统要求一天只能提现一次",
                                showCancel: false,
                                confirmText: "我知道了"
                        })
                        return
                }
                if (this.data.status == 5) {
                        wx.showModal({
                                title: '账号异常',
                                content: "请前往“我的->提现记录”里面联系客服",
                                showCancel: false,
                                confirmText: "我知道了",
                                success:function(){
                                        wx.navigateTo({
                                                url: '/pages/withdraw/withdraw',
                                        })
                                }
                        })
                        return
                }
                // 获取电话号码
                if (that.data.mobile == null || that.data.mobile == "") {
                        var that = this
                        wx.login({
                                success: function (res) {
                                        that.setData({
                                                wxcode: res.code
                                        })
                                }
                        })
                        this.setData({
                                sqshow: true
                        })
                        return
                }
                wx.showModal({
                        title: '提示',
                        content: '确认提现？',
                        showCancel: true,
                        success: function(res) {
                                if (res.confirm) {
                                        that.forbut()
                                } 
                        }
                })
               
        },

        suuc:function(res){
                console.log(res)
                if (res.message =="success"){
                        this.request()
                        this.setData({
                                tixianstatus:true
                        })
                        var header = {
                                'authorization': wx.getStorageSync('tekon'),
                                'content-type': "application/x-www-form-urlencoded"
                        }
                        util.formhttp(app.globalData.appurl +'/mission', 'get', '', header, this.statdata);
                }else{
                        wx.showToast({
                                title: '提现失败',
                                image: 'images/nas.png',
                                duration: 2000,
                                mask: true,
                        })
                }
        },

        quxiao:function(){
                this.setData({
                        sqshow: false
                })
        },
       
        quxiaoxian:function(){
                this.setData({
                        tixianstatus: false
                })
        },
        statdata:function(res){
                console.log(res)
                for (var i = 0; i < res.data.userMissions.newerMissions.length;i++){
                        if (res.data.userMissions.newerMissions[i].id == 9) {
                                var data = {
                                        missionId: res.data.userMissions.newerMissions[i].id
                                }
                                var header = {
                                        'authorization': wx.getStorageSync('tekon'),
                                        'content-type': "application/x-www-form-urlencoded"
                                }
                                util.formhttp(app.globalData.appurl + '/mission', 'post', data, header, (data)=>{
                                        var that = this
                                        if (data.data.missionAccomplished == 1) {
                                                that.setData({
                                                        toastshow: true,
                                                        contens: "奖励第一次提现的你",
                                                        jinbi: data.data.missionReward
                                                })
                                                setTimeout(function () {
                                                        that.setData({
                                                                toastshow: false
                                                        })
                                                }, 3000)
                                        }
                                });
                        }
                }  
        }
})